"""
halfcylinder.py --                                                                                            
                                                                                                                                                                                                        
It sets a cylinder's geometric properties.                                              
                                                                                                                                                                                                            
Date of creation: 2007-03-17                                                                        
                                                                                                                                                                                                
Copyright  Robotics and Automation Group, Pontificia Universidad Javeriana - Cali.                  
    Freddy Naranjo Perez, fnaranjo@puj.edu.co                                                                   
    Antonio Alejandro Matta Gomez amatta@puj.edu.co                                      
    Julian David Colorado, jdcolorado@puj.edu.co                           
    Juan Camilo Acosta Mejia, jcacosta@puj.edu.co                                    
                                                                                                                                                                                                        
See the file "license.terms" for information on usage and redistribution of this file, and for a    
DISCLAIMER OF ALL WARRANTIES.
"""

import vtk
from vtk.util.colors import *

class HalfCylinder:
  def __init__( self, ren, geomdim, posorient, lighting, color ):
        """ Inits a cylinder's 3d object and its associated color and lighting"""
        
        #Cylinder's configuration. 
        cylinder = vtk.vtkCylinder()
        cylinder.SetRadius(geomdim[1])
        topPlane = vtk.vtkPlane()
        topPlane.SetOrigin(0, geomdim[0], 0)
        topPlane.SetNormal(0, 1, 0)
        bottomPlane = vtk.vtkPlane()
        bottomPlane.SetOrigin(0, 0.0, 0)
        bottomPlane.SetNormal(0, -1, 0)
        backPlane = vtk.vtkPlane()
        backPlane.SetOrigin(0.0, 0.0, geomdim[1]/2)
        backPlane.SetNormal(0, 0, -1)
        
        #Cylinder's boolean function: Intersection between Cylinder
        #and the two planes.
        theCylinder = vtk.vtkImplicitBoolean()
        theCylinder.SetOperationTypeToIntersection()
        theCylinder.AddFunction(cylinder)
        theCylinder.AddFunction(topPlane)
        theCylinder.AddFunction(bottomPlane)
        theCylinder.AddFunction(backPlane)
        
        # The sample function generates a distance function from the implicit
        # function (which in this case is the CylinderHole). This is then contoured to
        # get a polygonal surface.
        theCylinderSample = vtk.vtkSampleFunction()
        theCylinderSample.SetImplicitFunction(theCylinder)
        #theCylinderSample.SetModelBounds(-0.1, 0.1, -0.1, geomdim[0]+0.01, -0.001, 0.1)
        theCylinderSample.SetModelBounds(-geomdim[1]-0.01, geomdim[1]+0.01, -0.1, geomdim[0]+0.01, -geomdim[1]-0.01, geomdim[1]+0.01)
        theCylinderSample.SetSampleDimensions(60, 60, 60)
        theCylinderSample.ComputeNormalsOn()
        
        #vtk.vtkContourFilter applied in order to get a polygonal surface.
        theCylinderSurface = vtk.vtkContourFilter()
        theCylinderSurface.SetInputConnection(theCylinderSample.GetOutputPort())
        theCylinderSurface.SetValue(0, 0.00005)

        #Decimation algorithm applied in order to optimaze polygonal surface.
        deci = vtk.vtkDecimatePro()
        deci.SetInputConnection(theCylinderSurface.GetOutputPort())
        deci.SetTargetReduction(0.70)
        deci.PreserveTopologyOn()

        #Smoothing applied in order to get a better surface.
        smooth= vtk.vtkSmoothPolyDataFilter()
        smooth.SetInputConnection(deci.GetOutputPort())
        smooth.SetNumberOfIterations(5)

        #Normal's computation.
        normals = vtk.vtkPolyDataNormals()
        normals.SetInputConnection(smooth.GetOutputPort())
        normals.FlipNormalsOn()

        #Normal's passed to the poly data mapper.
        cylinderMapper = vtk.vtkPolyDataMapper()
        cylinderMapper.SetInputConnection(normals.GetOutputPort())
        cylinderMapper.ScalarVisibilityOff()

        self.cylinderActor = vtk.vtkActor()
        self.cylinderActor.SetMapper(cylinderMapper)
        self.cylinderActor.GetProperty().SetInterpolationToGouraud()
        self.cylinderActor.GetProperty().SetColor(color)
        self.cylinderActor.GetProperty().SetSpecular(.3)
        self.cylinderActor.GetProperty().SetSpecularPower(30)
        self.cylinderActor.SetPosition( posorient[0], posorient[1], posorient[2] )
        self.cylinderActor.SetOrientation( posorient[3], posorient[4], posorient[5] )
       
            
        
    
        
    
    

        
        
            
        
    

    
  
  